package com.kedou.sdk.ui.assist;

import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.kedou.sdk.JuHaoWanSDK;
import com.kedou.sdk.core.HttpEngineApi;
import com.kedou.sdk.core.HttpEngineConduits;
import com.kedou.sdk.utils.ToolsUtils;

public class ThirdLoginUtil {
    private volatile static ThirdLoginUtil instance;
    private Context mycontext;
    private String mUuid;
    private OnThirdLoginListener mlistener;
    private ThirdLoginUtil() {
    }
    public static ThirdLoginUtil getInstance() {
        if (instance == null) {
            synchronized (JuHaoWanSDK.class) {
                if (instance == null) {
                    instance = new ThirdLoginUtil();
                }
            }
        }
        return instance;
    }

    public boolean isAppInstalled(Context context){
        return ToolsUtils.isInstallApp(context,"com.kd.juhaowan");
    }

    public void startPay(Context context){
        if(isAppInstalled(context)){
            HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
            Intent intent = new Intent();
            intent.putExtra("isPay",true);
            intent.putExtra("username",conduits.getBoxuser().getUsername());
            intent.putExtra("avatar",conduits.getBoxuser().getAvatar());

            intent.setAction("com.kd.juhaowan.pay");

            context.startActivity(intent);
        }else {
            HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
            String url = conduits.getPlatformConfigInfo().getBox_promote_url();
            try {
                Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                context.startActivity(in);

            } catch (ActivityNotFoundException exception) {
                Toast.makeText(context,"您还未安装聚好玩手游",Toast.LENGTH_SHORT).show();
            }

        }
    }

    public  void startLogin(Context context,String uuid,OnThirdLoginListener listener){
        this.mycontext = context;
        this.mlistener = listener;
        this.mUuid = uuid;
        if(isAppInstalled(context)){
//            Toast.makeText(context,"已安装",Toast.LENGTH_SHORT).show();
            Log.e("ThirdLogin","start thirdLogin");
            registr(context);
//            Intent intent = new Intent("com.gamebox.hnssn.login");
//            intent.setComponent(new ComponentName("com.gamebox.hnssn","com.gamebox.hnssn.ui.broad.ThirdReciver"));
//            intent.addFlags(32);
////            intent.setAction("com.gamebox.hnssn.login");
//            try {
//                ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
////                appName = (String) getPackageManager().getApplicationLabel(applicationInfo);
//                intent.putExtra("name",context.getPackageManager().getApplicationLabel(applicationInfo).toString());
//            } catch (PackageManager.NameNotFoundException e) {
//                e.printStackTrace();
//            }
//            context.sendBroadcast(intent);
            Intent intent = new Intent();
            intent.putExtra("uuid",mUuid);
            intent.setAction("com.kd.juhaowan.login");
            context.startActivity(intent);
        }else {
            HttpEngineConduits conduits = HttpEngineApi.getInstance().getEngineConduits();
            String url = conduits.getPlatformConfigInfo().getBox_promote_url();
            try {
                Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                context.startActivity(in);

            } catch (ActivityNotFoundException exception) {
                Toast.makeText(context,"您还未安装聚好玩手游",Toast.LENGTH_SHORT).show();
            }
        }
    }


    class MyBroadCastReciver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            int status =intent.getIntExtra("status",0);
            Log.e("ThirdLogin",intent.getAction()+"status"+status);
            if(status==0){
                if(mlistener!=null){
                    mlistener.doLoginFinish(0,null);
                }
                Toast.makeText(context,"登录取消",Toast.LENGTH_SHORT).show();
            }else if (status==1){
//                String accessToken = intent.getStringExtra("accessToken");
//                String refreshToken = intent.getStringExtra("refreshToken");
                String accessToken = intent.getStringExtra("code");
                Log.e("ThirdLogin accessToken",accessToken);
                HttpEngineConduits wanSDKDataDao = HttpEngineApi.getInstance().getEngineConduits();
                wanSDKDataDao.saveCurrentLoginUserTickit(accessToken);
                mlistener.doLoginFinish(1,accessToken);
            }


            unRegistr(mycontext);
        }
    }
    private MyBroadCastReciver myReciver;
    private void registr(Context context){
        Log.e("ThirdLogin","start registr ");
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.lysy.broad");
        myReciver=new MyBroadCastReciver();
        context.registerReceiver(myReciver,filter);
    }
    private void unRegistr(Context context){
        if(myReciver!=null){
            context.unregisterReceiver(myReciver);
        }

    }
}
